package com.ruoyi.project.hcy.product.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.product.domain.series.ProSeries;
import com.ruoyi.project.hcy.product.service.IProSeriesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProductSeriesController
 * @Author SAI
 * @Date 2020/9/17 12:00
 * @Description ProductSeriesController
 * @Version 1.0
 */
@Controller
@RequestMapping("/hcy/product/proSeries")
public class ProSeriesController extends BaseController {

    @Autowired
    private IProSeriesService productSeriesService;

    private String prefix = "hcy/product/series";

    /**
     * 页面的转发
     * @return
     */
    @RequiresPermissions("product:proSeries:view")
    @GetMapping()
    public String operlog() {
        return prefix + "/proSeries";
    }


    /**
     * 查询产品系列集合，包含分页、条件查询
     * @param proSeries
     * @return
     */
    @RequiresPermissions("product:proSeries:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProSeries proSeries) {
        startPage();
        List<ProSeries> list = productSeriesService.selectSeriesList(proSeries);
        return getDataTable(list);
    }

    /**
     * 添加页面的弹窗转发
     * @return
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存产品系类
     * @param proSeries
     * @return
     */
    @RequiresPermissions("product:proSeries:add")
    @Log(title = "产品系列管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated ProSeries proSeries) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(productSeriesService.checkseriesNameUnique(proSeries))) {
            return error("新增系列'" + proSeries.getSeriesName() + "'失败，系列名称已存在");
        }
        return toAjax(productSeriesService.insertProSeries(proSeries));
    }

    /**
     * 检测产品系列名字是否唯一
     * @param proSeries
     * @return
     */
    @PostMapping("/checkseriesNameUnique")
    @ResponseBody
    public String checkseriesNameUnique(ProSeries proSeries) {
        return productSeriesService.checkseriesNameUnique(proSeries);
    }

    /**
     * 修改产品系列，数据回显
     */
    @GetMapping("/edit/{seriesId}")
    public String edit(@PathVariable("seriesId") Long seriesId, ModelMap mmap) {
        mmap.put("series", productSeriesService.selectSeriesById(seriesId));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("product:proSeries:edit")
    @Log(title = "产品系列管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ProSeries proSeries) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(productSeriesService.checkseriesNameUnique(proSeries))) {
            return error("修改产品名称'" + proSeries.getSeriesName() + "'失败，系列名称已存在");
        }
        return toAjax(productSeriesService.updateProSeries(proSeries));
    }


    /**
     * 删除产品系列
     * @param ids
     * @return
     */
    @RequiresPermissions("product:proSeries:remove")
    @Log(title = "产品系列管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(productSeriesService.deleteProSeriesByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

}
